package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insert extends JInternalFrame {

	Connection con;
	DbTest dt;
	private JLabel lblNewLabel;
	private JLabel lblEa;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel lblNewLabel_1;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	public Insert(Connection c,DbTest dt) {
		super("input",true,true,true,false);
		setBounds(100, 100, 356, 242);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel_1());
		con=c;
		this.dt=dt;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("코드");
			lblNewLabel.setBounds(36, 23, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblEa(){
		if (lblEa == null) {
			lblEa = new JLabel("EA");
			lblEa.setBounds(36, 58, 57, 15);
		}
		return lblEa;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(105, 20, 154, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(105, 55, 154, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null){
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					try{
						String cod=code.getText();
						int Ea=Integer.parseInt(ea.getText());
						
						String sql="insert into ptbl(serial,code,ea) values(ptbl_serial.nextval,?,?)";
						PreparedStatement pstmt=con.prepareStatement(sql);
						pstmt.setString(1, cod);
						pstmt.setInt(2, Ea);
						
						pstmt.executeUpdate();
						pstmt.close();
						code.setText("");
						ea.setText("");
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
				}
			});
			btnNewButton.setBounds(95, 101, 97, 23);
		}
		return btnNewButton;
	}
	
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("New label");
			lblNewLabel_1.setBounds(12, 188, 231, 15);
		}
		return lblNewLabel_1;
	}
}
